package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.business.entity.AskedQuestion;
import cn.stylefeng.guns.modular.business.model.params.AskedQuestionParam;
import cn.stylefeng.guns.modular.business.service.AskedQuestionService;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;


/**
 * 常见问题控制器
 *
 * @author pzp
 * @Date 2019-07-29 19:57:58
 */
@Controller
@RequestMapping("/askedQuestion")
public class AskedQuestionController extends BaseController {

    private String PREFIX = "/business/askedQuestion";

    @Autowired
    private AskedQuestionService askedQuestionService;

    @Autowired
    private FileInfoService fileInfoService;

    @Value("${server.port}")
    private String port;

    /**
     * 跳转到主页面
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/askedQuestion.html";
    }

    /**
     * 新增页面
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/askedQuestion_add.html";
    }

    /**
     * 编辑页面
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @RequestMapping("/edit")
    public String edit(String questionId, Model model) {
        AskedQuestion detail = this.askedQuestionService.getById(questionId);
        model.addAttribute("askedQuestionDetail",detail);
        return PREFIX + "/askedQuestion_edit.html";
    }

    /**
     * 新增接口
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(AskedQuestionParam askedQuestionParam) {
        if (StringUtils.isNotBlank(askedQuestionParam.getContent())){
            askedQuestionParam.setContent(HtmlUtils.htmlUnescape(askedQuestionParam.getContent()));
        }
        this.askedQuestionService.add(askedQuestionParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(AskedQuestionParam askedQuestionParam) {
        this.askedQuestionService.update(askedQuestionParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(AskedQuestionParam askedQuestionParam) {
        this.askedQuestionService.delete(askedQuestionParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(AskedQuestionParam askedQuestionParam) {
        AskedQuestion detail = this.askedQuestionService.getById(askedQuestionParam.getQuestionId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author pzp
     * @Date 2019-07-29
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(AskedQuestionParam askedQuestionParam,String condition) {
        return this.askedQuestionService.findPageBySpec(askedQuestionParam,condition);
    }

}


